<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('放款记录') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('放款日期')" prop="returnVisitDate">
        <el-date-picker
          v-model="dateRange"
          style="width: 400px"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:financingLending:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dealFinancingLendingList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('放款日期')" align="center" prop="returnVisitDate" min-width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnVisitDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('放款金额') + $t('金额单位')" align="center" prop="lendersMoney" min-width="180">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.lendersMoney, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('还款账户')" align="center" prop="repaymentAccount" :show-overflow-tooltip="true" min-width="400" />

      <el-table-column :label="$t('放款凭证')" align="center" min-width="180">
        <template slot-scope="scope">
          <div v-if="scope.row.fileList !== null && scope.row.fileList !== '' && scope.row.fileList !== undefined && scope.row.fileList.length > 0" style="width: 100%;">
            <el-dropdown style="width: 180px">
              <span :title="scope.row.fileList[0].name" style="cursor: pointer;color: #3da4fc">
                {{ scope.row.fileList[0].name }}
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item com @click.native="onlinePreview(scope.row.fileList[0])">{{ $t('预览') }}</el-dropdown-item>
                <el-dropdown-item @click.native="downLoadFileOpens(scope.row.fileList[0].id)">{{ $t('下载') }}</el-dropdown-item>
                <el-dropdown-item @click.native="deleteFile(scope.row)">{{ $t('删除') }}</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
          <div v-if="scope.row.fileList === null || scope.row.fileList === '' || scope.row.fileList === undefined || scope.row.fileList.length <= 0">
            <uploadSingle
              ref="uploadSingle"
              :data-type="'deal_financing_lending'"
              :business-id="queryParams.dealId"
              upload-style="color: #fff;background-color: #67c23a;cursor: pointer;border-radius: 4px;border: 1px solid #67c23a;width: 65px;height: 25px;"
              :data-table-row-num="scope.$index"
              :upload-btn-name="$t('上传')"
              @onFileSuccess="onFileSuccess"
            />
          </div>
        </template>
      </el-table-column>

      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" min-width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:financingLending:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['deal:financingLending:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改放款记录对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body @close="getList">
      <dealFinancingLendingForm ref="dealFinancingLendingForm" />
    </el-dialog>
    <OnlinePreview ref="OnlinePreview" />
  </div>
</template>

<script>
import { listDealFinancingLending, delDealFinancingLending, exportDealFinancingLending, saveDealFinancingLending } from '@/api/deal/financingProjects/dealFinancingLending'
import dealFinancingLendingForm from './dealFinancingLendingForm.vue'
import uploadSingle from '@/components/File/Upload/single.vue'
import { delFile } from '@/api/file/fileBase'
import { downLoadFileOpen } from '@/utils/zipdownload'
export default {
    components: {
        dealFinancingLendingForm,
        uploadSingle
    },
  data() {
      return {
          dateRange: [],
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 放款记录表格数据
          dealFinancingLendingList: [],
          // 弹出层标题
          title: '放款记录',
          // 是否显示弹出层
          formOpen: false,
          // 查询参数
          queryParams: {
              dealId: this.$route.query.id,
              beginTime: undefined,
              endTime: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {}
      }
  },
  created() {
      this.getList()
  },
  methods: {
    /** 查询放款记录列表 */
    getList() {
        this.loading = true
      if (this.dateRange != null && this.dateRange.length > 1) {
        this.queryParams.beginTime = this.parseTime(this.dateRange[0])
        this.queryParams.endTime = this.parseTime(this.dateRange[1])
      } else {
        this.queryParams.startDate = undefined
        this.queryParams.endDate = undefined
      }
        listDealFinancingLending(this.queryParams).then(response => {
            this.dealFinancingLendingList = response.rows
            this.total = response.total
            this.loading = false
        }).catch(() => {
            this.loading = false
        })
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            dealId: undefined,
            returnVisitDate: undefined,
            lendersMoney: undefined,
            repaymentAccount: undefined,
            delFlag: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.queryParams.beginTime = undefined
      this.queryParams.endTime = undefined
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealFinancingLendingForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealFinancingLendingForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return delDealFinancingLending(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(() => {
            this.loading = false
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return exportDealFinancingLending(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(() => {
        })
    },
    onFileSuccess(that, file, rowIndex) {
      this.$nextTick(() => {
        this.dealFinancingLendingList[rowIndex].fileList = [{ id: file.fileId, name: file.fileName }]
        saveDealFinancingLending(this.dealFinancingLendingList[rowIndex]).then(response => {
          if (response.code === 200) {
            this.msgSuccess(this.$t('保存成功'))
            this.getList()
          } else {
            this.msgError(response.msg)
          }
          this.loading = false
        })
      })
    },
    // 预览附件
    onlinePreview(row) {
      this.$refs.OnlinePreview.onlinePreviewFile(row.id)
    },
    deleteFile(row) {
      this.loading = true
      delFile(row.fileList[0].id).then(response => {
        if (response.code === 200) {
          row.fileList = null
          this.msgSuccess(response.msg)
        } else {
          this.msgError(response.msg)
        }
        this.loading = false
      })
    },
    downLoadFileOpens(id) {
      downLoadFileOpen(id)
    }
  }
}
</script>
